//
//获取解密后的global-metadata.dat
//github:https://github.com/350030173/global-metadata_dump
//
//用法：
//frida -U -l global-metadata_dump.js packagename
//
//导出的文件在/data/data/yourPackageName/global-metadata.dat
//
//get_self_process_name()获取当前运行进程包名
//参考：https://github.com/lasting-yang/frida_dump/blob/master/dump_dex_class.js
//
//
function get_self_process_name() {
    var openPtr = Module.getExportByName('libc.so', 'open');
    var open = new NativeFunction(openPtr, 'int', ['pointer', 'int']);

    var readPtr = Module.getExportByName("libc.so", "read");
    var read = new NativeFunction(readPtr, "int", ["int", "pointer", "int"]);

    var closePtr = Module.getExportByName('libc.so', 'close');
    var close = new NativeFunction(closePtr, 'int', ['int']);

    var path = Memory.allocUtf8String("/proc/self/cmdline");
    var fd = open(path, 0);
    if (fd != -1) {
        var buffer = Memory.alloc(0x1000);

        var result = read(fd, buffer, 0x1000);
        close(fd);
        result = ptr(buffer).readCString();
        return result;
    }

    return "-1";
}


function frida_Memory(){
    const pattern = '03 10 80 E7 03 20 80 C7';
    const pattern1 = '00 00 00 00 ?? 13 37 ?? 42';

    Memory.scan(m.base, m.size, pattern, {
        onMatch(address, size) {
            console.log('Memory.scan() found match at', address,
                'with size', size);

            // Optionally stop scanning early:
            return 'stop';
        },
        onComplete() {
            console.log('Memory.scan() complete');
        }
    });
}
function frida_Memory(pattern)
{
    console.log("头部标识:" + pattern);
    //枚举内存段的属性,返回指定内存段属性地址
    var addrArray = Process.enumerateRanges("r--");

    for (var i = 0; i < addrArray.length; i++)
    {
        var addr = addrArray[i];
       // console.log(addr.base);
        Memory.scan(addr.base, addr.size, pattern, {
            onMatch(address, size) {
                console.log('Memory.scan() found match at', address,
                    'with size', size);

                // Optionally stop scanning early:
                return 'stop';
            },
            onComplete() {

               // console.log('Memory.scan() complete');
            }
        });

    }



}

// CDF87DD8 - 03 10 80 E7           - STR R1,[R0, R3]
// CDF87DDC - 03 20 80 C7           - STRGT R2,[R0, R3]
// CDF87DE0 - 02 00 00 DA           - BLE CDF87DF0
// setImmediate(frida_Memory("03 10 A0 E3 00 10 80 E5"));
setImmediate(frida_Memory("0C 70 85 E7 01 00 A0 E3"));
//frida -U com.mfkj.tkjd.aligames -l tkdz.js
// frida -H 192.168.5.209:6666 com.mfkj.tkjd.aligames -l tkdz.js
// frida -H 192.168.5.209:6666 com.popcap.pvz -l tkdz.js